﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Gaiocorp.SLMapViewer
{
    public partial class MapToolBar : UserControl
    {
        private IDictionary<string, string> _parameters = null;
        // Initialize vars
       // MapControlBase mc = new MapControlBase();
       // MapControl mc = new MapControl(null);
        public MapToolBar()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(Page_Loaded);            
        }

        void Page_Loaded(object sender, RoutedEventArgs e)
        {

            //mc = ParentMap;
            //if (this.ParentMap.GetType() == typeof(MapControlKiosk))
            //{ mc = (MapControlKiosk)ParentMap; }
            //else if (ParentMap.GetType() == typeof(MapControl))
            //{ mc = (MapControl)ParentMap; }
           
        }
        #region Private Members
        private MapControl _ParentMap;
       
       
        #endregion

        #region Public Properties
        public IDictionary<string, string> Parameters
        {
            get { return _parameters; }
            set { _parameters = value; }
        }

        public MapControl ParentMap
        {
            get { return _ParentMap; }
            set { _ParentMap = value; }
        }
       
        #endregion

        private void Button_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {


            // Get children controls
            TextBox stxtRequest = (TextBox)this.ParentMap.FindName("stxtRequest");
            Grid gridLegend = (Grid)this.ParentMap.FindName("gridLegend");
            Canvas cnvLegend = (Canvas)this.ParentMap.FindName("cnvLegend");
            Image MapImg = (Image)this.ParentMap.FindName("MapImg");
            Border brdQuery = (Border)this.ParentMap.FindName("brdQuery");
            Border brdResults = (Border)this.ParentMap.FindName("brdResults");
            TextBox stxtHasSelection = (TextBox)this.ParentMap.FindName("stxtHasSelection");
            TextBox stxtActiveLayerName = (TextBox)this.ParentMap.FindName("stxtActiveLayerName");
            Border brdPrint = (Border)ParentMap.FindName("brdPrint");

            Image currentTool = (Image)sender;
            string strName = currentTool.Name;
            // Inside a try/catch in case tool does not have a border
            // e.g. pan buttons in kiosk mode
            try
            {
                Rectangle currentRect = (Rectangle)this.FindName(strName + "_rect");
                // Create a blue and a black Brush
                SolidColorBrush yellowBrush = new SolidColorBrush();
                yellowBrush.Color = Colors.Yellow;
                // Fill rectangle with blue color
                currentRect.Fill = yellowBrush;
            }
            catch (Exception)
            { }

            switch (strName)
            {
                case "FixZoomInImg":
                    if (stxtRequest.Text == "<REQUEST action='ZoomIn' sessionid='" + _parameters["SessionID"] + "' />")
                    {
                        ParentMap.stxtRequest_TextChanged(stxtRequest, null);
                    }
                    else
                        stxtRequest.Text = "<REQUEST action='ZoomIn' sessionid='" + _parameters["SessionID"] + "' />";

                    break;
                case "ZoomInImgKiosk":
                    if (stxtRequest.Text == "<REQUEST action='ZoomIn' sessionid='" + _parameters["SessionID"] + "' />")
                    {
                        ParentMap.stxtRequest_TextChanged(stxtRequest, null);
                    }
                    else
                        stxtRequest.Text = "<REQUEST action='ZoomIn' sessionid='" + _parameters["SessionID"] + "' />";

                    break;
                case "FixZoomOutImg":
                    if (stxtRequest.Text == "<REQUEST action='ZoomOut' sessionid='" + _parameters["SessionID"] + "' />")
                    {
                        ParentMap.stxtRequest_TextChanged(stxtRequest, null);
                    }
                    else
                        stxtRequest.Text = "<REQUEST action='ZoomOut' />";
                    break;
                case "ZoomOutImgKiosk":
                    if (stxtRequest.Text == "<REQUEST action='ZoomOut' sessionid='" + _parameters["SessionID"] + "' />")
                    {
                        ParentMap.stxtRequest_TextChanged(stxtRequest, null);
                    }
                    else
                        stxtRequest.Text = "<REQUEST action='ZoomOut' sessionid='" + _parameters["SessionID"] + "' />";
                    break;
                case "PanRightKiosk":
                    if (stxtRequest.Text == @"<REQUEST ymax='" + (MapImg.Height / 2).ToString()
                                            + "' xmax='" + ((MapImg.Width / 2) - (MapImg.Width / 4)).ToString()
                                            + "' ymin='" + (MapImg.Height / 2).ToString()
                                            + "' xmin='" + (MapImg.Width / 2).ToString()
                                            + "' action='Pan' sessionid='"
                                            + _parameters["SessionID"] + "' />")
                    {
                        ParentMap.stxtRequest_TextChanged(stxtRequest, null);
                    }
                    else
                        stxtRequest.Text = "<REQUEST ymax='" + (MapImg.Height / 2).ToString()
                                         + "' xmax='" + ((MapImg.Width / 2) - (MapImg.Width / 4)).ToString()
                                         + "' ymin='" + (MapImg.Height / 2).ToString()
                                         + "' xmin='" + (MapImg.Width / 2).ToString()
                                         + "' action='Pan' sessionid='"
                                         + _parameters["SessionID"] + "' />";
                    break;
                case "PanLeftKiosk":
                    if (stxtRequest.Text == "<REQUEST ymax='" + (MapImg.Height / 2).ToString()
                                          + "' xmax='" + ((MapImg.Width / 2) + (MapImg.Width / 4)).ToString()
                                          + "' ymin='" + (MapImg.Height / 2).ToString()
                                          + "' xmin='" + (MapImg.Width / 2).ToString()
                                          + "' action='Pan' sessionid='"
                                          + _parameters["SessionID"] + "' />")
                    {
                        ParentMap.stxtRequest_TextChanged(stxtRequest, null);
                    }
                    else
                        stxtRequest.Text = "<REQUEST ymax='" + (MapImg.Height / 2).ToString()
                                         + "' xmax='" + ((MapImg.Width / 2) + (MapImg.Width / 4)).ToString()
                                         + "' ymin='" + (MapImg.Height / 2).ToString()
                                         + "' xmin='" + (MapImg.Width / 2).ToString()
                                         + "' action='Pan' sessionid='"
                                         + _parameters["SessionID"] + "' />";
                    break;
                case "PanDownKiosk":
                    if (stxtRequest.Text == "<REQUEST ymax='" + ((MapImg.Height / 2) - (MapImg.Height / 4)).ToString()
                                         + "' xmax='" + (MapImg.Width / 2).ToString()
                                         + "' ymin='" + (MapImg.Height / 2).ToString()
                                         + "' xmin='" + (MapImg.Width / 2).ToString()
                                         + "' action='Pan' sessionid='"
                                         + _parameters["SessionID"] + "' />")
                    {
                        ParentMap.stxtRequest_TextChanged(stxtRequest, null);
                    }
                    else
                        stxtRequest.Text = "<REQUEST ymax='" + ((MapImg.Height / 2) - (MapImg.Height / 4)).ToString()
                                         + "' xmax='" + (MapImg.Width / 2).ToString()
                                         + "' ymin='" + (MapImg.Height / 2).ToString()
                                         + "' xmin='" + (MapImg.Width / 2).ToString()
                                         + "' action='Pan' sessionid='"
                                         + _parameters["SessionID"] + "' />";
                    break;
                case "PanTopKiosk":
                    if (stxtRequest.Text == "<REQUEST ymax='" + ((MapImg.Height / 2) + (MapImg.Height / 4)).ToString()
                                         + "' xmax='" + (MapImg.Width / 2).ToString()
                                         + "' ymin='" + (MapImg.Height / 2).ToString()
                                         + "' xmin='" + (MapImg.Width / 2).ToString()
                                         + "' action='Pan' sessionid='"
                                         + _parameters["SessionID"] + "' />")
                    {
                        ParentMap.stxtRequest_TextChanged(stxtRequest, null);
                    }
                    else
                        stxtRequest.Text = "<REQUEST ymax='" + ((MapImg.Height / 2) + (MapImg.Height / 4)).ToString()
                                         + "' xmax='" + (MapImg.Width / 2).ToString()
                                         + "' ymin='" + (MapImg.Height / 2).ToString()
                                         + "' xmin='" + (MapImg.Width / 2).ToString()
                                         + "' action='Pan' sessionid='"
                                         + _parameters["SessionID"] + "' />";
                    break;
                case "PrevImg":
                    if (stxtRequest.Text == "<REQUEST action='ZoomLast' sessionid='" + _parameters["SessionID"] + "' />")
                    {
                        ParentMap.stxtRequest_TextChanged(stxtRequest, null);
                    }
                    else
                        stxtRequest.Text = "<REQUEST action='ZoomLast' sessionid='" + _parameters["SessionID"] + "' />";
                    break;
                case "InitImg":
                case "ZoomFullImgKiosk":
                    if (stxtRequest.Text == "<REQUEST action='FullExtent' sessionid='" + _parameters["SessionID"] + "' />")
                    {
                        ParentMap.stxtRequest_TextChanged(stxtRequest, null);
                    }
                    else
                        stxtRequest.Text = "<REQUEST action='FullExtent' sessionid='" + _parameters["SessionID"] + "' />";
                    break;
                case "RemovePntImg":
                    if (stxtRequest.Text == "<REQUEST action='RemovePin' sessionid='" + _parameters["SessionID"] + "' />")
                    {
                        ParentMap.stxtRequest_TextChanged(stxtRequest, null);
                    }
                    else
                        stxtRequest.Text = "<REQUEST action='RemovePin' sessionid='" + _parameters["SessionID"] + "' />";
                    break;
                case "LegendImg":
                    LegendControl legend = (LegendControl)ParentMap.FindName("legend");
                    if (legend.gridLegend.Tag == null)
                    {
                        if (_parameters["SessionID"] != null)
                        {
                            stxtRequest.Text = "<REQUEST action='GetLegend' sessionid='" + _parameters["SessionID"] + "' />";
                        }
                        else
                        {
                            stxtRequest.Text = "<REQUEST action='GetLegend' sessionid='" + _parameters["SessionID"] + "' />";
                        }
                    }
                    if (_parameters["InternalControls"].ToString() == "1")
                    {
                        if (legend.cnvLegend.Visibility == Visibility.Collapsed)
                        {
                            legend.cnvLegend.SetValue(Canvas.TopProperty, Convert.ToDouble(30));
                            legend.cnvLegend.SetValue(Canvas.LeftProperty, Convert.ToDouble(5));
                        }
                        legend.cnvLegend.Visibility = Visibility.Visible;
                    }

                    break;
                case "QueryAttrsImg":
                    if (brdQuery.Visibility == Visibility.Collapsed)
                    {
                        brdQuery.SetValue(Canvas.TopProperty, Convert.ToDouble(30));
                        brdQuery.SetValue(Canvas.LeftProperty, Convert.ToDouble(5));
                    }
                    brdQuery.Visibility = Visibility.Visible;
                    break;
                case "TabImg":
                    if (brdResults.Visibility == Visibility.Collapsed)
                    {
                        brdResults.SetValue(Canvas.TopProperty, Convert.ToDouble(30));
                        brdResults.SetValue(Canvas.LeftProperty, Convert.ToDouble(5));
                    }
                    if (stxtHasSelection.Text == "no")
                    {
                        if (stxtActiveLayerName.Text != string.Empty)
                        {
                            stxtRequest.Text = "<REQUEST action='GetAttrTable' layer='" + stxtActiveLayerName.Text + "' sessionid='" + _parameters["SessionID"] + "' />";
                        }
                    }
                    brdResults.Visibility = Visibility.Visible;
                    break;
                case "PrintImg":
                    brdPrint.Visibility = Visibility.Visible;
                    break;
            }
        }
        private void Button_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            // Inside a try/catch in case tool does not have a border
            // e.g. pan buttons in kiosk mode
            try
            {
                Image currentTool = (Image)sender;
                string strName = currentTool.Name;
                Rectangle currentRect = (Rectangle)this.FindName(strName + "_rect");
                SolidColorBrush LightGreyBrush = new SolidColorBrush();
                LightGreyBrush.Color = Colors.Black;
                // Fill rectangle with blue color
                currentRect.Fill = LightGreyBrush;
                TextBox stxtActiveTool = (TextBox)ParentMap.FindName("stxtActiveTool");
                stxtActiveTool.Text = string.Empty;
            }
            catch (Exception)
            { }

        }
        private void Tool_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            // Get children controls to use
            TextBox stxtActiveTool = (TextBox)ParentMap.FindName("stxtActiveTool");
            Image MapImg = (Image)ParentMap.FindName("MapImg");

            Image currentTool = (Image)sender;
            string strName = currentTool.Name;
            Rectangle currentRect = (Rectangle)this.FindName(strName + "_rect");
            SolidColorBrush yellowBrush = new SolidColorBrush();
            yellowBrush.Color = Colors.Yellow;
            // Fill rectangle with blue color
            currentRect.Fill = yellowBrush;

            foreach (UIElement el in cnvToolbar.Children)
            {
                string strElName = el.GetValue(FrameworkElement.NameProperty).ToString();
                if (el.GetType() == typeof(Rectangle))
                {
                    Rectangle ar = (Rectangle)el;
                    if (ar.Name != currentRect.Name)
                    {
                        SolidColorBrush GreyBrush = new SolidColorBrush();
                        GreyBrush.Color = Colors.Black;
                        // Fill rectangle with blue color
                        ar.Fill = GreyBrush;
                    }
                }
            }
            switch (strName)
            {
                case "PanImg":
                    stxtActiveTool.Text = "PAN";
                    MapImg.Cursor = Cursors.Hand;
                    break;
                case "ZoomInImg":
                    stxtActiveTool.Text = "ZOOMIN";
                    MapImg.Cursor = Cursors.Arrow;
                    break;
                case "ZoomOutImg":
                    stxtActiveTool.Text = "ZOOMOUT";
                    MapImg.Cursor = Cursors.Arrow;
                    break;
                case "SelectImg":
                    stxtActiveTool.Text = "SELECT";
                    MapImg.Cursor = Cursors.Arrow;
                    break;
                case "InfoImg":
                    stxtActiveTool.Text = "INFO";
                    MapImg.Cursor = Cursors.Arrow;
                    break;
                case "AddPntImg":
                case "CreatePntImg":
                case "AddPinKiosk":
                    stxtActiveTool.Text = "ADDPIN";
                    MapImg.Cursor = Cursors.Arrow;
                    break;
                case "HyperlImg":
                    stxtActiveTool.Text = "HYPERLINK";
                    MapImg.Cursor = Cursors.Arrow;
                    break;
                case "MeasureImg":
                    Border brdBorder = (Border)ParentMap.FindName("brdMeasureToolTip");
                    brdBorder.SetValue(Canvas.TopProperty, 26.0);
                    brdBorder.SetValue(Canvas.LeftProperty, 0.0);
                    brdBorder.Visibility = Visibility.Visible;
                    stxtActiveTool.Text = "MEASURE";
                    MapImg.Cursor = Cursors.Arrow;
                    break;
            }
        }

        private void PanLeftKiosk_MouseEnter(object sender, MouseEventArgs e)
        {
            Image img = (Image)sender;
            img.Opacity = 1.0;
        }

        private void PanLeftKiosk_MouseLeave(object sender, MouseEventArgs e)
        {
            Image img = (Image)sender;
            img.Opacity = 0.5;
        }

    }
}
